package org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.http.advanced;


import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.NettyByteBuf;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.NettyTransferChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.server.AbstractHandleHttpReportClientProxyClientTransferChannelInitSuccessfulTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.constant.ProxyMessageType;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;
import org.springframework.stereotype.Component;


/**
 * 目标客户端传输通道连接初始化成功
 * HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CHANNEL_INIT_SUCCESSFUL_
 */
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Slf4j
@Component
public class ServerHandleHttpReportClientProxyClientTransferChannelInitSuccessfulTypeAdvanced
        extends AbstractHandleHttpReportClientProxyClientTransferChannelInitSuccessfulTypeAdvanced<NettyProxyMsg> {


    /**
     * 处理当前数据
     *
     * @param nettyChannelContext 当前通道
     * @param nettyProxyMsg   通道数据
     */
    @Override
    public void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {

        Channel channel = nettyChannelContext.channel();
        byte[] msgVisitorId = nettyProxyMsg.getVisitorId();

        byte[] msgClientTargetIp = nettyProxyMsg.getClientTargetIp();
        byte[] msgClientTargetPort = nettyProxyMsg.getClientTargetPort();
        byte[] clientId = nettyProxyMsg.getClientId();


        // next translation
        Channel nextTransferChannel = NettyTransferChannelContext.getVisitor(msgVisitorId);
        ChannelAttributeKeyUtils.buildTransferNextChannel(nextTransferChannel, channel);
        ChannelAttributeKeyUtils.buildTransferNextChannel(channel, nextTransferChannel);
        ChannelAttributeKeyUtils.buildClientId(channel, clientId);
        ChannelAttributeKeyUtils.buildVisitorId(channel, msgVisitorId);
        NettyByteBuf nettyByteBufData = ChannelAttributeKeyUtils.getNettyByteBufData(nextTransferChannel);


        log.info("准备发送代理请求数据到客户端");
        NettyProxyMsg clientConnectTagetNettyProxyMsg = new NettyProxyMsg();
        clientConnectTagetNettyProxyMsg.setVisitorId(msgVisitorId);

        clientConnectTagetNettyProxyMsg.setClientTargetIp(msgClientTargetIp);
        clientConnectTagetNettyProxyMsg.setClientTargetPort(msgClientTargetPort);
        clientConnectTagetNettyProxyMsg.setClientId(clientId);
        clientConnectTagetNettyProxyMsg.setData(nettyByteBufData.getData());

        clientConnectTagetNettyProxyMsg.setType(ProxyMessageType.HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_REQUEST_);

        channel.writeAndFlush(clientConnectTagetNettyProxyMsg);


    }

}
